{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载数据\n",
    "from sklearn.datasets import fetch_openml\n",
    "mnist = fetch_openml('mnist_784')\n",
    "# 标签和训练数据\n",
    "X, y = mnist['data'], mnist['target']\n",
    "\n",
    "# 训练集和测试集建立\n",
    "X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]\n",
    "\n",
    "import numpy as np\n",
    "# 训练集洗牌赋值\n",
    "shuffle_index = np.random.permutation(60000)\n",
    "X_train, y_train = X_train[shuffle_index], y_train[shuffle_index]\n",
    "# 测试集洗牌赋值\n",
    "shuffle_index=np.random.permutation(10000)\n",
    "X_test,y_test=X_test[shuffle_index],y_test[shuffle_index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAPMklEQVR4nO3df6hVdbrH8c+jadBPfx3FnFNOQ1Fxs+O0kwsTQ5fhDmWERmE/wAzsnumHMEMT3KjIAv+ovBYWNWApaphWNFJR3FuKFhM0uDWvqXlv3jB/cNRjIaN/mc5z/zir4VRnf9dxr7332vq8X3DYe69nr70el35c+6zv3utr7i4Ap78hZTcAoDUIOxAEYQeCIOxAEIQdCOKMVm5szJgxPnHixFZuEghl165dOnTokA1UKxR2M7te0kJJQyW94u5PpZ4/ceJEVavVIpsEkFCpVGrW6n4bb2ZDJb0o6QZJV0i6w8yuqPf1ADRXkd/Zp0ja6e5fufsxSaskTWtMWwAarUjYJ0ja0+/x3mzZD5hZt5lVzaza29tbYHMAimj62Xh3X+TuFXevdHR0NHtzAGooEvZ9kjr7Pf5ZtgxAGyoS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1omvG22Xf1TH9eFCfofupad/+lpBskPZC9XW1L3vc7WDuNnQ5qGu9WGWCa8X8oc9/VO/15UWWEfZ+kzn6Pf5Ytawvuvi+7PShptdpvKuoD38+gm90eLLmff2inabwHmmZcbbDvypz+vIywb5B0iZn93MyGS7pd0jsl9PETZnZ2duJEZna2pN+q/aaifkfSrOz+LElvl9jLD7TLNN61phlXyfuu9OnP3b3lP5Kmqu+M/P9JerSMHmr0dbGk/85+tpXdm6SV6ntb9536zm3MljRa0lpJX0paI2lUG/X2qqTPJW1RX7DGl9Tbtep7i75F0ubsZ2rZ+y7RV0v2Gx+XBYLgBB0QBGEHgiDsQBCEHQiCsANBEHYgCMIOBPH/bKqyi8dEWm4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 选择标签\n",
    "some_digit=X_train[23456]\n",
    "some_digit_img=some_digit.reshape(28,28)\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "plt.imshow(some_digit_img,cmap=matplotlib.cm.binary)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型选择\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "# 多标签分类 拿出为6的标签\n",
    "y_train_6=(y_train==6)\n",
    "# 算出距离\n",
    "kn_clf = KNeighborsClassifier()\n",
    "kn_clf.fit(X_train,y_train_6)\n",
    "some_digit=X_train[23456]\n",
    "some_digit=[some_digit]\n",
    "# 测试指定数据\n",
    "kn_clf.predict(some_digit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 网格搜索\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "param_grid = [{'weights': [\"uniform\", \"distance\"], 'n_neighbors': [3, 4, 5]}]\n",
    "\n",
    "knn_clf = KNeighborsClassifier()\n",
    "grid_search = GridSearchCV(knn_clf, param_grid, cv=5, verbose=3, n_jobs=-1)\n",
    "grid_search.fit(X_train, y_train)\n",
    "print(grid_search.best_params_)\n",
    "print(grid_search.best_score_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试集验证\n",
    "from sklearn.metrics import accuracy_score\n",
    "y_pred = grid_search.predict(X_test)\n",
    "accuracy_score(y_test, y_pred)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
